
//格式化时间
function formatNumber (n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}
export function formatTime (date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2}`
}
//倒计时
export function interval (endtime){
  var date1 = new Date(); 
  var date2 = new Date(endtime); 
  var date3 = date2.getTime() - date1.getTime();   //时间差的毫秒数 
   //计算出相差天数
   var days=Math.floor(date3/(24*3600*1000))
 
   //计算出小时数

   var leave1=date3%(24*3600*1000)    //计算天数后剩余的毫秒数
   var hours=Math.floor(date3/(3600*1000))
   //计算相差分钟数
   var leave2=leave1%(3600*1000)        //计算小时数后剩余的毫秒数
   var minutes=Math.floor(leave2/(60*1000))
   //计算相差秒数
   var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
   var seconds=Math.round(leave3/1000)
   if(date3<=0) return false
   return days+'-'+hours+'-'+minutes+'-'+seconds
}


export function checkPhone(phone){
  if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){ 
    return false; 
 } else{
  return true; 
 }
}
export function checkNumber(num){
  if(!(/^[0-9]*$/.test(num))){ 
    return false; 
 } else{
  return true; 
 }
}

export default {
  formatNumber,
  formatTime,
  interval,
  checkPhone,
  checkNumber
}
